package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName:
 * @Description:
 * @version: v1.1.0
 * @author: shuaiqin
 * @date: 2021/8/26-14:17
 * <p>
 * Modification History:
 * Date         Author          Version            Description
 * ------------------------------------------------------------
 * 2021/8/26          shuaiqin          v1.1.0               修改原因
 */
@RestController
@Slf4j
public class PaymentController {
    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String servePort;

    @Resource
    private DiscoveryClient discoveryClient;

    @PostMapping(value = "/payment/create")
    public CommonResult addPayment(@RequestBody Payment payment){
        int result = paymentService.addPayment(payment);
        log.info("**********插入结果："+result);
        if(result>0){
            return new CommonResult(200,"插入成功 servePort:"+servePort,result);
        }else {
            return new CommonResult(400,"插入失败 servePort:"+servePort,null);
        }
    }

    @GetMapping(value = "/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("**********GET查询结果："+payment);
        if(payment!=null){
            return new CommonResult(200,"GET查询成功 servePort:"+servePort,payment);
        }else {
            return new CommonResult(400,"GET查询失败 servePort:"+servePort,null);
        }
    }
    @GetMapping(value = "/payment/lb/{id}")
    public CommonResult getPaymentLb(@PathVariable("id") Long id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("**********LB查询结果："+payment);
        if(payment!=null){
            return new CommonResult(200,"LB查询成功 serverPort:"+servePort,payment);
        }else {
            return new CommonResult(400,"LB查询失败 servePort:"+servePort,null);
        }
    }
    @GetMapping(value = "/payment/discovery")
    public  Object discovery(){
        List<String> services = discoveryClient.getServices();//获取在eureka上注册的微服务名称Application
        for (String service : services) {
            log.info("*********************"+service);
        }
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance instance : instances) {
            log.info("查看在当前特定服务下有多少实例"+instance.getInstanceId()+"\t"+instance.getHost()+"\t"
                    +instance.getPort()+"\t"+instance.getUri());
        }
        return this.discoveryClient;
    }
    @GetMapping(value = "/payment/feign/notTimeout")
    public String payment(){
        return servePort;
    }
    @GetMapping(value = "/payment/feign/timeout")
    public String paymentFeignTimeout(){
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return servePort;
    }
    @GetMapping(value = "/payment/zipkin")
    public String paymentZipkin(){
        return "hi ,zipkin!";
    }
}
